//
//  UIImage+Video.m
//  NvSellerShow
//
//  Created by Meicam on 2017/2/8.
//  Copyright © 2017年 Meicam. All rights reserved.
//

#import <AVFoundation/AVFoundation.h>
#import "UIImage+Video.h"


@implementation UIImage (Video)
+ (UIImage *)imageWithVideoUrl:(NSURL *)videoUrl seekPos:(NSInteger)seekPos{
    AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:videoUrl options:nil];
    AVAssetImageGenerator* gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMake(seekPos, 10);
    NSError* error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return thumb;
}

+ (UIImage *) imageWithVideoUrl:(NSURL *)videoUrl atTime:(CGFloat)time withTimeBase:(CGFloat)timeBase {
    AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:videoUrl options:nil];
    AVAssetImageGenerator* gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    CMTime timestamp = CMTimeMake(time/(timeBase/25), 25);
    NSError* error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:timestamp actualTime:&actualTime error:&error];
    UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return thumb;
}

@end
